package com.cxyd.modules.wcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cxyd.common.constant.Constants;
import com.cxyd.common.constant.RcsConstants;
import com.cxyd.common.constant.WcsConstants;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.common.utils.ProcResult;
import com.cxyd.common.utils.StringUtils;
import com.cxyd.modules.api.dto.RcsResultHK;
import com.cxyd.modules.log.service.SysLogWorkService;
import com.cxyd.modules.wcs.dao.AgvTrkHkDao;
import com.cxyd.modules.wcs.dao.PlcItemDao;
import com.cxyd.modules.wcs.dto.*;
import com.cxyd.modules.wcs.entity.*;
import com.cxyd.modules.wcs.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.*;

@Service
@Slf4j
public class AgvTrkHkServiceImpl extends CrudServiceImpl<AgvTrkHkDao, AgvTrkHkEntity, AgvTrkHkDTO> implements AgvTrkHkService {
    @Resource
    private TrkService trkService;
    @Resource
    private RouteSetService routeSetService;
    @Resource
    private StnMappingService stnMappingService;
    @Resource
    private PlcItemService plcItemService;
    @Resource
    private PcItemService pcItemService;
    @Resource
    private SysLogWorkService logWorkService;
    @Resource
    private PostTcpService postTcpService;
    @Resource
    private PlcItemDao plcItemDao;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public AgvTrkHkServiceImpl() {
    }

    @Override
    public QueryWrapper<AgvTrkHkEntity> getWrapper(Map<String, Object> params) {
        String trkId = (String) params.get("trkId");
        String ctnrCode = (String) params.get("ctnrCode");
        String taskCode = (String) params.get("taskCode");
        QueryWrapper<AgvTrkHkEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(trkId), "trk_id", trkId);
        wrapper.eq(StringUtils.isNotBlank(ctnrCode), "ctnr_code", ctnrCode);
        wrapper.eq(StringUtils.isNotBlank(taskCode), "task_code", taskCode);
        wrapper.orderByDesc("create_date");
        return wrapper;
    }

    @Override
    public void hkTrkScan(TrkEntity wcsTrk) {
//        0.解析任务
        if (wcsTrk == null) {
            return;
        }
        String workName = "[流程-任务扫描]-HK";
        String workProc = "AgvTrkHkServiceImpl.hkTrkScan";
        String pCode = WcsConstants.PROC_CODE_SUCCESS;
        String pMsg;
//        1.通过trkId尝试获取搬送任务是否存在
        AgvTrkHkEntity agvTrkHkEntity = getAgvTrkByTrkId(wcsTrk.getTrkId());
        if (agvTrkHkEntity != null) {
            //记录日志并返回
            pCode = "hkTrkScan_001";
            pMsg = "任务扫描异常!托盘[" + wcsTrk.getContNo() + "]任务已存在!";
            wcsTrk.setTrkSt(WcsConstants.TRK_STATUS_ERROR);
            wcsTrk.setComments(pMsg);
            wcsTrk.setUpdateDate(new Date());
            trkService.updateById(wcsTrk);
            logWorkService.workLogging(workName, wcsTrk.getContNo(), pCode, pMsg, "", workProc, WcsConstants.WCS_USER_NAME, 10L);
            return;
        }
        Long trkId = wcsTrk.getTrkId();
//        搬送路径与模板匹配
        RouteSetEntity routeSetByPath = routeSetService.getRouteSetByPath(wcsTrk.getTrkType(), wcsTrk.getFrmPos(), wcsTrk.getToPos());
        String contNo = wcsTrk.getContNo();
//        起始站点与目标站点均为实际站点,无需转换
        String positionPath = routeSetByPath.getPositionPath();
//        位置类型：00-位置编号 03-货架编号 05-仓位编号
        String positionType = routeSetByPath.getPositionType();
//        数据写入[AGV_TRK_HK]表(taskCode使用有含义的编码规则:托盘条码+年月日+6位trkId)
        String taskCode = String.valueOf(System.currentTimeMillis());
        String taskTyp = routeSetByPath.getTaskTyp();
//        防止reqCode重复，使用料箱码+时间戳
        String reqCode = contNo + System.currentTimeMillis();
//        任务优先级
        int trkPrty = wcsTrk.getTrkPrty();
//        2.写入agv_trk_yf数据
        AgvTrkHkEntity agvTrkHkNew = new AgvTrkHkEntity();
        agvTrkHkNew.setTrkId(trkId);
//        wbCode用于记录工作台位置
        agvTrkHkNew.setPriority(WcsConstants.TRK_PRTY_DEF);
        agvTrkHkNew.setReqCode(reqCode);
        agvTrkHkNew.setTaskCode(taskCode);
        agvTrkHkNew.setTaskType(taskTyp);
        agvTrkHkNew.setPositionCode(positionPath);
        agvTrkHkNew.setPositionType(positionType);
//        CTU/叉车装载的容器编号(无需写入料架号,只需要写入类型1)
        agvTrkHkNew.setCtnrCode(contNo);
        agvTrkHkNew.setCtnrTyp("1");
//        潜伏AGV的容器编号
//        agvTrkHkNew.setPodCode(contNo);
        agvTrkHkNew.setPodType("1");
//        将任务类型写入AGV_TYP中
        agvTrkHkNew.setTaskMode("0");
        agvTrkHkNew.setPriority(String.valueOf(trkPrty));
        agvTrkHkNew.setCreator(WcsConstants.WCS_USER_ID);
        baseDao.insert(agvTrkHkNew);
//        3.更新wcs_trk数据
        pMsg = "容器[" + contNo + "]生成[HK-AGV]搬送任务！关联ID[" + agvTrkHkNew.getId() + "]";
        wcsTrk.setTrkSt(WcsConstants.TRK_STATUS_RUNNING);
        wcsTrk.setComments(pMsg);
        wcsTrk.setUpdateDate(new Date());
        trkService.updateById(wcsTrk);
        logWorkService.workLogging(workName, wcsTrk.getContNo(), pCode, pMsg, "", workProc, WcsConstants.WCS_USER_NAME, 0);
    }

    @Override
    public AgvTrkHkPostDTO getPostHkTrk() {
//      1.获取需下发的任务(入库任务需要一个一个下发,优先下料再上料)
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getStatus, 0)
                .orderBy(true, true, AgvTrkHkEntity::getCreateDate)
                .last("limit 1");
        AgvTrkHkEntity agvTrkHK = baseDao.selectOne(qw);
        if (agvTrkHK == null) {
            return null;
        }
        String positionType = agvTrkHK.getPositionType();
        AgvTrkHkPostDTO postInfo = ConvertUtils.sourceToTarget(agvTrkHK, AgvTrkHkPostDTO.class);
//      规划搬送路径(不再通过配置,直接从起始与结束位置获取)
        List<String> paths = Arrays.asList(agvTrkHK.getPositionCode().split("#"));
        List<PositionPathDTO> positionCodePath = new ArrayList<>();
        paths.forEach(item -> {
            PositionPathDTO path = new PositionPathDTO();
            path.setType(positionType);
            path.setCode(item);
            positionCodePath.add(path);
        });
        postInfo.setTargetRoute(positionCodePath);
        postInfo.setRobotTaskCode(agvTrkHK.getTaskCode());
        postInfo.setInitPriority(agvTrkHK.getPriority());
        return postInfo;
    }

    @Override
    public ApplyPassDTO getContinueHkTrk() {
        //1.获取请求输送线状态的任务
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getStatus, RcsConstants.HK_STATUS_CONTINUE)
                .orderBy(true, true, AgvTrkHkEntity::getUpdateDate)
                .last("limit 1");
        AgvTrkHkEntity agvTrkHK = baseDao.selectOne(qw);
        if (agvTrkHK == null) {
            return null;
        }
        //判断plc信号是取货还是防货
        TrkEntity trkEntity = trkService.selectById(agvTrkHK.getTrkId());
        StnMappingEntity startStn = stnMappingService.getStnByNo(trkEntity.getFrmPos());
        StnMappingEntity endStn = stnMappingService.getStnByNo(trkEntity.getToPos());
        String curPos = "";
        String actionStatus = "";
        //如果起点属于positionCode设备，则当前站点为起点，否则为终点
        if (startStn.getWmsStation().equals(agvTrkHK.getCurrentPositionCode())) {
            curPos = startStn.getWcsStation();
            actionStatus = "5";//5-允许取货
        } else if (endStn.getWmsStation().equals(agvTrkHK.getCurrentPositionCode())) {
            curPos = endStn.getWcsStation();
            actionStatus = "6";//6-允许放货
        }
        QueryWrapper<PlcItemEntity> qwPlc = new QueryWrapper<>();
        qwPlc.lambda().eq(PlcItemEntity::getItemGrp, "PLC_STATUS").eq(PlcItemEntity::getItemCode, curPos).last("limit 1");
        PlcItemEntity plcItemEntity = plcItemDao.selectOne(qwPlc);
        //3.生成任务继续信号
        ApplyPassDTO passDTO = new ApplyPassDTO();
        passDTO.setEqptCode(agvTrkHK.getCurrentPositionCode());
        passDTO.setTaskCode(agvTrkHK.getTaskCode());
        passDTO.setActionStatus(actionStatus);
        return passDTO;
    }

    @Override
    public int getAgvTrkCount() {
        return baseDao.getAgvTrkSumByState();
    }

    @Override
    public AgvTrkHkEntity getAgvTrkByTrkId(long id) {
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getTrkId, id)
                .last("limit 1");
        return baseDao.selectOne(qw);
    }

    @Override
    public AgvTrkHkEntity getAgvTrkByContNo(String contNo) {
        if (StringUtils.isEmpty(contNo)) {
            return null;
        }
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getCtnrCode, contNo)
                .last("limit 1");
        return baseDao.selectOne(qw);
    }

    @Override
    public AgvTrkHkEntity getAgvTrkByTaskCode(String taskCode) {
        if (StringUtils.isEmpty(taskCode)) {
            return null;
        }
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getTaskCode, taskCode)
                .ne(AgvTrkHkEntity::getStatus, RcsConstants.AGV_TRK_HK_CANCEL)
                .last("limit 1");
        return baseDao.selectOne(qw);
    }

    @Override
    public void agvTrkHandle(AgvTrkHkPostDTO dto, RcsResultHK response) {
//        1.判断任务是否下发成功
        String msg = response.getMessage();
//        3.定义状态
        String pCode = WcsConstants.PROC_CODE_SUCCESS;
        String pMsg = "搬送任务成功下发[HK-AGV]!任务编号[" + dto.getRobotTaskCode() + "]";
        int status;
        if ("SUCCESS".equals(response.getCode())) {
            status = RcsConstants.HK_STATUS_POST;
//            成功下发更新[wcs_trk]任务状态

            //下发成功后需通知 MES
            try {
                PostTcpDTO postDTO = new PostTcpDTO();
//        更新任务状态
                AgvTrkHkEntity agvTrkByTaskCode = this.getAgvTrkByTaskCode(dto.getRobotTaskCode());
                TrkEntity wcsTrk = trkService.selectById(agvTrkByTaskCode.getTrkId());
                postDTO.setPostSt(WcsConstants.POST_STATUS_START);
                postDTO.setNoticeType("0");
                postDTO.setFrmPos(wcsTrk.getFrmPos());
                postDTO.setEndPos(wcsTrk.getToPos());
                postDTO.setContNo(dto.getCtnrCode());
                postDTO.setTaskCode(dto.getRobotTaskCode());
                postDTO.setTaskTyp(dto.getTaskType());
                postTcpService.save(postDTO);
            } catch (Exception e) {
                pCode = "agvTrkHandle_002";
                pMsg = "创建postTcp指令失败！:" + e.getMessage();
            }

        } else {
            status = RcsConstants.HK_STATUS_ERROR;
            pCode = "agvTrkHandle_001";
            pMsg = "搬送任务下发失败!rcs反馈异常:" + msg;
        }
//        开始时间-start
        Instant start = Instant.now();
//        4.条件更新语句
        UpdateWrapper<AgvTrkHkEntity> uw = new UpdateWrapper<>();
        uw.lambda().set(AgvTrkHkEntity::getStatus, status)
                .set(AgvTrkHkEntity::getData, msg)
                .set(AgvTrkHkEntity::getReqTime, dateFormat.format(new Date()))
                .set(AgvTrkHkEntity::getUpdater, WcsConstants.WCS_USER_ID)
                .set(AgvTrkHkEntity::getUpdateDate, new Date())
                .eq(AgvTrkHkEntity::getTaskCode, dto.getRobotTaskCode());
        baseDao.update(null, uw);
//        结束时间-end
        Instant end = Instant.now();
//        5.日志记录
        String workName = "[流程-任务下发]-HK";
        String workProc = "AgvTrkHkServiceImpl.agvTrkHandle";
//        计算耗时
        Duration times = Duration.between(start, end);
        logWorkService.workLogging(workName, dto.getRobotTaskCode(), pCode, pMsg, "", workProc, WcsConstants.WCS_USER_NAME, times.toMillis());
    }

    @Override
    public void agvContinueHandle(ApplyPassDTO dto, RcsResultHK response) {
//        1.成功下发更新任务状态为执行中,失败则记录失败原因
//        if (StringUtils.isEmpty(response.getData().getRobotTaskCode())) {
//            return;
//        }
        String msg = response.getMessage();
//        通过taskCode获取取放申请的任务
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getTaskCode, dto.getTaskCode()).last("limit 1");
        AgvTrkHkEntity agvTrk = baseDao.selectOne(qw);
//        清除卡控信号
        TrkEntity trkEntity = trkService.selectById(agvTrk.getTrkId());
        StnMappingEntity startStn = stnMappingService.getStnByNo(trkEntity.getFrmPos());
        StnMappingEntity endStn = stnMappingService.getStnByNo(trkEntity.getToPos());
        StnMappingEntity curStn = new StnMappingEntity();
        //如果起点属于positionCode设备，则当前站点为起点，否则为终点
        if (startStn.getWmsStation().equals(agvTrk.getCurrentPositionCode())) {
            curStn = startStn;
        } else if (endStn.getWmsStation().equals(agvTrk.getCurrentPositionCode())) {
            curStn = endStn;
        }
        pcItemService.updateValueByNo(curStn.getTargetAgv(), "0");

        String pCode = "0";
        String pMsg = "站点[" + curStn.getWcsStation() + "]安全交互通过!对应卡控点[" + curStn.getTargetAgv() + "]信号清[0]!下发RCS任务继续!";
//        无论成功还是失败都任务继续
        int status = RcsConstants.HK_STATUS_TAKE;
//        if (Constants.SUCCESS.equals(response.getCode())) {
////            成功下发更新[wcs_trk]任务状态
//        } else {
////            下发异常后继续下发
//            pCode = "continue_error_001";
//            pMsg = "站点[" + agvTrk.getCurrentPositionCode() + "]的任务继续下发异常!RCS反馈:" + msg;
//        }

//        更新agv_trk状态
        agvTrk.setStatus(status);
        agvTrk.setData(pMsg);
        agvTrk.setUpdateDate(new Date());
        baseDao.updateById(agvTrk);
        String workName = "[流程-任务继续]-HK";
        String workProc = "AgvTrkHkServiceImpl.agvContinueHandle";
        logWorkService.workLogging(workName, agvTrk.getTaskCode(), pCode, pMsg, "", workProc, WcsConstants.WCS_USER_NAME, 0);
    }

    @Override
    public void updateAgvTrkByContNo(String contNo) {
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getPodCode, contNo)
                .last("limit 1");
        AgvTrkHkEntity AgvTrkHkEntity = baseDao.selectOne(qw);
        if (AgvTrkHkEntity == null) {
            return;
        }
//        1.转移历史表
        baseDao.backupAgvTrk(contNo);
        baseDao.deleteById(AgvTrkHkEntity);
    }

    @Override
    public void updateAgvTrkByTaskCode(String taskCode) {
//        1.转移历史表
        baseDao.backupAgvTrkByTaskCode(taskCode);
//        2.删除表数据
        baseDao.deleteAgvTrkByTaskCode(taskCode);
    }

    @Override
    public void updateStatusByPermission() {
//        查找是否有叉车任务需要授权(status>30)
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().between(AgvTrkHkEntity::getStatus, RcsConstants.HK_STATUS_LOAD, RcsConstants.HK_STATUS_LEAVE)
                .orderBy(true, true, AgvTrkHkEntity::getUpdateDate)
                .last("limit 1");
        AgvTrkHkEntity agvTrk = baseDao.selectOne(qw);
        if (agvTrk == null) {
            return;
        }
        String workName = "[流程-安全许可]-PLC";
        String workProc = "AgvTrkHkServiceImpl.updateStatusByPermission";

//        根据任务所在当前站点判断机台是否许可作业
        TrkEntity trkEntity = trkService.selectById(agvTrk.getTrkId());
        StnMappingEntity startStn = stnMappingService.getStnByNo(trkEntity.getFrmPos());
        StnMappingEntity endStn = stnMappingService.getStnByNo(trkEntity.getToPos());
        StnMappingEntity curStn;
        //如果起点属于positionCode设备，则当前站点为起点，否则为终点
        if (startStn.getWmsStation().equals(agvTrk.getCurrentPositionCode())) {
            curStn = startStn;
        } else if (endStn.getWmsStation().equals(agvTrk.getCurrentPositionCode())) {
            curStn = endStn;
        } else {
            String code = "permission_error_001";
            String msg = "设备[" + agvTrk.getCurrentPositionCode() + "]未在[wcs_stn_mapping]中与相应站台配置!";
//            更新agv_trk状态
            agvTrk.setStatus(RcsConstants.HK_STATUS_ERROR);
            agvTrk.setData(code);
            agvTrk.setUpdateDate(new Date());
            baseDao.updateById(agvTrk);
            logWorkService.workLogging(workName, agvTrk.getTaskCode(), code, msg, "", workProc, WcsConstants.WCS_USER_NAME, 0);
            return;
        }
//        if (curStn == null) {
//            String code = "permission_error_001";
//            String msg = "站点[" + agvTrk.getCurrentPositionCode() + "]未在[wcs_stn_mapping]中配置!";
////            更新 agv_trk状态
//            agvTrk.setStatus(RcsConstants.HK_STATUS_ERROR);
//            agvTrk.setData(code);
//            agvTrk.setUpdateDate(new Date());
//            baseDao.updateById(agvTrk);
//            logWorkService.workLogging(workName, agvTrk.getTaskCode(), code, msg, "", workProc, WcsConstants.WCS_USER_NAME, 0);
//            return;
//        }
        //        获取搬送任务
//        TrkEntity wcsTrk = trkService.selectById(agvTrk.getTrkId());
        int status = agvTrk.getStatus();
        String signal = "1";
//        根据当前搬送任务的状态判断不同信号点
        if (RcsConstants.HK_STATUS_LEAVE == status) {
            signal = "3";
        }

//        获取需要判断的item_no信息(Arrived判断外卡控,Leave判断内卡控)
        String itemNo = curStn.getTargetPlc();
//        信号判断-PLC外卡控的值是否允许(Leave判定4的值)
        if (!plcItemService.signalJudgeByNo(itemNo, signal)) {
            String code = "no_permission";
            String msg = "站点[" + curStn.getWcsStation() + "]需求卡控[" + itemNo + "]信号为[" + signal + "],未通过,等待";
            agvTrk.setData(msg);
            agvTrk.setUpdateDate(new Date());
            baseDao.updateById(agvTrk);
            logWorkService.workLogging(workName, agvTrk.getTaskCode(), code, msg, "", workProc, WcsConstants.WCS_USER_NAME, 0);
            return;
        }

//        更新agv_trk任务状态
        agvTrk.setStatus(RcsConstants.HK_STATUS_CONTINUE);

        String code = "0";
        String msg = "站点[" + agvTrk.getCurrentPositionCode() + "]需求卡控[" + itemNo + "]信号为[" + signal + "]!验证通过!!!";

        agvTrk.setData(msg);
        agvTrk.setUpdateDate(new Date());
        baseDao.updateById(agvTrk);
        logWorkService.workLogging(workName, agvTrk.getTaskCode(), code, msg, "", workProc, WcsConstants.WCS_USER_NAME, 0);
    }

    @Override
    public ProcResult trkHandler(Map<String, Object> params) {
        String id = (String) params.get("id");
        String operateType = (String) params.get("operateType");
//        判断任务是否存在
        AgvTrkHkEntity agvTrkHkEntity = baseDao.selectById(id);
        if (agvTrkHkEntity == null) {
            return new ProcResult().result("agvTrkHandler_001", "AGV任务[" + id + "]不存在!");
        }
//        任务重发
        if (WcsConstants.TASK_FORCE_REPEAT_OPERATE.equals(operateType)) {
//            删除当前[agv_trk_hk]中的任务
            baseDao.deleteById(agvTrkHkEntity);

//            还需要将wcs_trk的空满标记重置
            TrkEntity trkEntity = trkService.selectById(agvTrkHkEntity.getTrkId());
            trkEntity.setTrkSt(0);
            trkEntity.setIsFull("1");
            trkEntity.setComments("任务单重新下发!");
            trkEntity.setUpdateDate(new Date());
            trkService.updateById(trkEntity);

            return new ProcResult().result(WcsConstants.PROC_CODE_SUCCESS, "任务单[" + agvTrkHkEntity.getTaskCode() + "]被强制重新下发成功!");
        } else if (WcsConstants.TASK_FORCE_REPEAT_CONTINUE.equals(operateType)) {
//        强制继续
            agvTrkHkEntity.setStatus(RcsConstants.HK_STATUS_CONTINUE);
            agvTrkHkEntity.setData("任务单强制继续!");
            agvTrkHkEntity.setUpdateDate(new Date());
            baseDao.updateById(agvTrkHkEntity);
            return new ProcResult().result(WcsConstants.PROC_CODE_SUCCESS, "任务单[" + agvTrkHkEntity.getTaskCode() + "]强制继续成功!");
        } else {

        }
        return new ProcResult().result(WcsConstants.PROC_CODE_SUCCESS, "任务单[" + agvTrkHkEntity.getTaskCode() + "]操作成功!");
    }

    @Override
    public AgvCancelHkDTO getCancelHkTrk() {
        //1.获取请求输送线状态的任务
        QueryWrapper<AgvTrkHkEntity> qw = new QueryWrapper<>();
        qw.lambda().eq(AgvTrkHkEntity::getStatus, RcsConstants.AGV_TRK_AWAIT_CANCEL)
                .last("limit 1");
        AgvTrkHkEntity agvTrkHK = baseDao.selectOne(qw);
        if (agvTrkHK == null) {
            return null;
        }
        AgvCancelHkDTO dto = new AgvCancelHkDTO();
        dto.setReqCode(UUID.randomUUID().toString().replaceAll("-", ""));
        dto.setReqTime(dateFormat.format(new Date()));
        dto.setTaskCode(agvTrkHK.getTaskCode());
        dto.setForceCancel("0");
//        dto.setAgvCode(agvTrkHK.getAgvCode());
        return dto;
    }

    @Override
    public void agvCancelHandle(AgvCancelHkDTO dto, RcsResultHK respond) {
        if (StringUtils.isEmpty(respond.getRobotTaskCode())) {
            return;
        }
        //1.获取任务单
        String taskCode = dto.getTaskCode();
        AgvTrkHkEntity agvTrkHK = baseDao.selectOne(new QueryWrapper<AgvTrkHkEntity>().lambda().eq(AgvTrkHkEntity::getTaskCode, taskCode));
        if (agvTrkHK == null) {
            return;
        }
        //2.更新任务单状态
        agvTrkHK.setStatus(RcsConstants.AGV_TRK_HK_CANCEL);
        agvTrkHK.setData(respond.getMessage());
        agvTrkHK.setUpdateDate(new Date());
        baseDao.updateById(agvTrkHK);
        //3.记录日志
        String workName = "[流程-任务取消]-HK";
        String workProc = "AgvTrkHkServiceImpl.agvCancelHandle";
        String code = "0";
        String msg = "AGV任务[" + taskCode + "]取消成功!";
        logWorkService.workLogging(workName, taskCode, code, msg, "", workProc, WcsConstants.WCS_USER_NAME, 0);
    }
}
